package com.tongcao.cn.netty.bio;

import com.tongcao.cn.util.CalculatorUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @author:tongcao
 * @since:2019/7/8 16:44
 * Description:server 任务处理类
 */
public class BioHanlder implements Runnable {

    private Socket socket;

    public BioHanlder(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        BufferedReader reader = null;
        PrintWriter writer = null;

        try {
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new PrintWriter(socket.getOutputStream(), true);
            String body = null;
            while (true) {
                body = reader.readLine();
                if (body == null) {
                    break;
                }
                System.out.println("server服务端接收参数 : " + body);
                writer.println(body + " = " + CalculatorUtil.cal(body).toString());

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }
}